import sys
# 将子模块的路径添加到 Python 的模块搜索路径中
sys.path.append('../dxt')

from lin_base import *
from config import config
from g_share import g_share
from lin_RA import add_lin_RA
from lin_ecl import build_ecl_ras, add_lin_ecl
from lin_jieqi import add_lin_jieqi
from lin_sign import add_lin_sign
from lin_month_zhe import add_lin_month_zhe
from lin_dates import add_lin_dates
from lin_weeks import add_lin_weeks
from lin_lunars import add_lin_lunars
from lin_sky_ecl import draw_sky_ecl
from lin_sky_mw import draw_milkyway_lin
from ut_lin_star import draw_fix_stars

def build_lin_frame(paper, year, tz=8):
    #from PIL import Image, ImageColor,ImageDraw,ImageFont,ImageOps
    from config import config
    font = unicode_font_44
    #year=2024
    #tz=8
    build_ecl_ras(year,tz)
    #paper = PAPER("A4L")
    draw = paper.draw
    x0= int(10*MM_UNIT)
    y00= int(16*MM_UNIT)
    
    x_tick = x0- int(1*MM_UNIT)
    print('x_tick:%s x0:%s 1mm:%s' % (x_tick,x0,MM_UNIT))
    x_label =x0 -int(3*MM_UNIT)    
    
    #title='赤經'
    #draw.text((x0,y00-int(2*MM_UNIT)),title,font=font,fill=RED)
    title='赤緯'
    draw.text((x_label-int(MM_UNIT),g_share.y01-int(9*MM_UNIT)),title,font=font,fill=BLACK)
    title=''
    xend=add_lin_RA(paper,title,x0,y00,font=font)
    xr_end=xend # - int(3* MM_UNIT) # for graph
    xr_tick = xr_end + int(1*MM_UNIT)
    xr_label = xr_end + int(1.2*MM_UNIT)
    title=''
    y01 = paper.max_y - Y_MM*10
    y_equ = int((y01-y00) /2) + y00
    y_1deg =int((y_equ -y00)/ 90)
    
    init_linplot(x0,xr_end,y00,y01,y_equ)
    
    s_year=year
    title='公曆'
    y= y00- Y_MM*1
    add_lin_dates(paper,title,x0,y,xend,s_year,font=font)
    
    draw.rectangle([(x0,y00),(xr_end,y01)],fill=config.color_sky_day)
    # vertical line on left
    draw.line([(x0,y00),(x0,y01)],fill=BLACK,width=2)
    # vertical line on right
    draw.line([(xr_end,y00),(xr_end,y01)],fill=BLACK,width=2)
    # draw equator
    draw.line([(x0,y_equ),(xend,y_equ)],fill=RED,width=2)
    draw.line([(x0,y_equ),(x_tick,y_equ)],fill=RED,width=2)
    draw.text((x_label,y_equ),'0',font=font,fill=RED)
    # vertical scale on left side
    for dec in [10,20,30,40,50,60,70,80]:
        y = int(y_equ - dec* y_1deg)
        draw.line([(x0,y),(x_tick,y)],fill=BLACK,width=2)
        draw.text((x_label,y), '-%s' % dec, font=font, fill=BLACK)
    for dec in [10,20,30,40,50,60,70,80]:
        #dec= -vdec
        y = int(y_equ + dec* y_1deg)
        draw.line([(x0,y),(x_tick,y)],fill=BLACK,width=2)
        draw.text((x_label,y), '%s' % dec, font=font, fill=BLACK)
    # vertical scale on right side
    for dec in [10,20,30,40,50,60,70,80]:
        y = int(y_equ - dec* y_1deg)
        draw.line([(xr_end,y),(xr_tick,y)],fill=BLACK,width=2)
        draw.text((xr_label,y), '-%s' % dec, font=font, fill=BLACK)
    for dec in [10,20,30,40,50,60,70,80]:
        #dec= -vdec
        y = int(y_equ + dec* y_1deg)
        draw.line([(xr_end,y),(xr_tick,y)],fill=BLACK,width=2)
        draw.text((xr_label,y), '%s' % dec, font=font, fill=BLACK)    
    y0=y01
    title='赤經'
    xend=add_lin_RA(paper,title,x0,y0,font=font)
    #draw.line([(x0,y0),(xend,y0)],fill=BLACK,width=2)
    
    x0= int(10*MM_UNIT)
    y1= int(y0+Y_MM) # for eclong
    y2= int(y0+Y_MM*2) # for jieqi
    y3= int(y0+Y_MM*3) # for sign
    y4= int(y0+Y_MM*4) # for zhe month
    y5= int(y0+Y_MM*5) # for date
    y6= int(y0+Y_MM*6) # for lunar date
    y7= int(y0+Y_MM*7) # for week
    
    title = '黃經'
    add_lin_ecl(paper,title,x0,y1,xend,font=font)
    title='節氣'
    add_lin_jieqi(paper,title,x0,y2,xend,font=font)
    title='星宮'
    add_lin_sign(paper,title,x0,y3,xend,font=font)
    title='月建'
    add_lin_month_zhe(paper,title,x0,y4,xend,font=font)
    
    #s_year=year

    title='朔望'
    add_lin_lunars(paper,title,x0,y5,xend,s_year,draw_rect=YELLOW,font=font)
    
    title='公曆'
    add_lin_dates(paper,title,x0,y6,xend,s_year,font=font)
        
    title='星期'
    add_lin_weeks(paper,title,x0,y7,xend,s_year,font=font)
        
    paper.draw_outline()
    paper.draw_MAX_RECT()

    return y7
            